package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.*;

/**
 * Write a description of class ProduktUI here.
 * 
 * @author Team Black
 * @version (a version number or a date)
 */
public class ProduktUI
{
    private ProductCtr productCtr;
    private Reader reader;
    
    public ProduktUI()
    {

    }
    /**
     * Start menu
     */
    public void StartProdukt()
    {
        boolean exit=false;
        reader = new Reader();
        productCtr = new ProductCtr();
        
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeMainMenu();
            if(choise == 1)
            {
                createLoan();
            }
            else{
                    if(choise == 2)
                    { 
                        showLoan();
                    }
                    else{
                        if(choise == 3)
                        {
                           updateLoanInfo();
                        }//end if
                        else{
                            if(choise == 4)
                            {
                                deleteLoan();
                            }//end if
                            else
                                if(choise ==5)
                                {
                                   createProduct();
                                }
                                else{
                                    if(choise == 6)
                                    {
                                       showProduct(); 
                                    }
                                        else
                                        {
                                            if(choise == 7)
                                            {
                                                updateProduct();
                                            }
                                            else
                                            {
                                               if(choise == 8)
                                               {
                                                    deleteProduct();
                                                }
                                                else
                                                {
                                                    if(choise == 9)
                                                    {
                                                        writeEnd();
                                                        exit = true;
                                                    }
                                                else
                                                {
                                                    if(!exit)
                                                    {
                                                        System.out.print("forkert input, prøv igen.");
                                                        pause();
                                                    }//end if
                                                }//end else
                                            }//end else
                                    }//end else
                                }//end else
                            }//end else
                        }//end else
                    }// end else
                }//en else
            }//end while
    }//end start
    /**
     * Write  start menu
     */
   private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Vare- og udlejningshåndtering***");
            System.out.println(" (1) Opret udlejningsvare");
            System.out.println(" (2) Vis udlejningsvare");
            System.out.println(" (3) Opdater udlejningsvare");
            System.out.println(" (4) Slet udlejningsvare ");
            System.out.println(" (5) Opret vare");
            System.out.println(" (6) Vis vare");
            System.out.println(" (7) Opdater vare");
            System.out.println(" (8) Slet vare");
            System.out.println(" (9) Tilbage");
            System.out.print("\n Tryk for at komme videre: ");
            int choise = 0;
            try
            {
                choise = keyboard.nextInt();
            }
            catch(Exception e)
            {
                 System.out.println("\nInput skal være et tal, prøv igen.");
                 pause();
                 writeMainMenu();
            }
          return choise;
    }
    /**
     * Creating Loan
     */
   private void createLoan()
   {
        System.out.println("Under konstruktion");
        pause();
   }
     /**
     * Show Loan
     */  
   private void showLoan()//måske String? det ser vi lige på senere
   {
       System.out.println("Under konstruktion");
       pause();
   }
       /**
     * Update Loan
     */
   private void updateLoanInfo()
   {
       System.out.println("Under konstruktion");    
       pause();
   }
       /**
     * Delete Loan
     */
   private void deleteLoan()
   {
       System.out.println("Under konstruktion");
       pause();
   }
       /**
     * Edit Loan Conditions
     */
   private void editLoanConditions()
   {
       System.out.println("Under konstruktion");
       pause();
   }
       /**
     * Creating Product
     */
   private void createProduct()
   {
        System.out.println();
        System.out.println("   Opret ny vare   ");
        System.out.println();
        
        String _name = reader.inputProductName();
        int _id = reader.inputProductId();
        double _purchasePrice = reader.inputProductPurchasePrice();
        double _price = reader.inputProductPrice();
        String _description = reader.inputProductDecription();
        
        productCtr.createProduct(_id, _description, _purchasePrice, _price, _name);
        Product product = productCtr.searchProductById(_id);
        System.out.println();
        System.out.println(product.getName() + " er blevet oprettet uden id: " + product.getId());
   }
          /**
     * Show Product
     */
   private void showProduct()
   {
        System.out.println();
        System.out.println("   Du kan søge vare ud fra Navn eller ID   ");
        System.out.println();
        
        boolean exit = false;

        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeSearchMenu();
            if(choise == 1)
            {
               String _name = reader.inputProductName();
                
               Product product = productCtr.searchProductByName(_name);
                
               System.out.println("Navn        : " +product.getName());
               System.out.println("ID          : " +product.getId());
               System.out.println("Indkøbspris : " +product.getPurchasePrice());
               System.out.println("Pris        : " +product.getPrice());
               System.out.println(" Beskrivelse: " +product.getDescription());

               pause();
            }
            else
            {
            if(choise == 2)
            { 
               int _id = reader.inputProductId();
               Product product = productCtr.searchProductById(_id);
                
               System.out.println("Navn        : " +product.getName());
               System.out.println("ID          : " +product.getId());
               System.out.println("Indkøbspris : " +product.getPurchasePrice());
               System.out.println("Pris        : " +product.getPrice());
               System.out.println(" Beskrivelse: " +product.getDescription());

               pause();
            }
            else
            {
                writeEnd();
                exit = true;
            }//end else
            }//end else
        }//end while
       
   }
          /**
     * Update Product
     */
   private void updateProduct()
   {
       System.out.println("   Opdatere Vare ud fra Navn eller ID ");
       System.out.println();
       boolean exit=false;
       

       while (!exit) //! means while exit not is true (that is: false)
       {
            int choise = writeupdateMenu();
            if(choise == 1)
            {
               String _name = reader.inputProductName();
                
               Product currproduct = productCtr.searchProductByName(_name);
               
               System.out.println("Indskriv nu de nye ændringer");
               System.out.println();
               
               int _id = reader.inputProductId();
               String _description = reader.inputProductDecription();
               double _purchasePrice = reader.inputProductPurchasePrice();
               double _price = reader.inputProductPrice();
                   
               productCtr.updateProductByName(_name, _id, _description, _purchasePrice, _price);
               

               System.out.println("Varen er opdateret");
               pause();
            }
            else
            {
            if(choise == 2)
            { 
               int _id = reader.inputProductId();
               Product product = productCtr.searchProductById(_id);
               
               System.out.println("Indskriv nu de nye ændringer");
               System.out.println();
               
               String _name = reader.inputProductName();
               String _description = reader.inputProductDecription();
               double _purchasePrice = reader.inputProductPurchasePrice();
               double _price = reader.inputProductPrice();
               
               productCtr.updateProductById(_id, _description, _purchasePrice, _price, _name);
               System.out.println("Varen er opdateret");
               pause();
            }
            else
            {
                writeEnd();
                exit = true;
            }//end else
            }//end else
       }//end while
   }
          /**
     * Delete Product
     */
   public void deleteProduct()
   {
       System.out.println();
        System.out.println("   Du kan slette vare ud fra Navn eller ID   ");
        System.out.println();
        
        boolean exit = false;
        int _id = 0;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeSearchMenu();
            
            if(choise == 1)
            {
               String _name = reader.inputProductName();
               Product product = productCtr.searchProductByName(_name);
               _id = product.getId(); 
               pause();
            }
            else
            {
            if(choise == 2)
            { 
               _id = reader.inputProductId();
               Product product = productCtr.searchProductById(_id);
               _id = product.getId(); 
               pause();
            }
            else
            {
                writeEnd();
                exit = true;
            }//end else
            }//end else
        }//end while
       productCtr.deleteProduct(_id);
   }
          /**
     *  WriteEnd
     */
   private void writeEnd()
   {
        System.out.println(" Systemet slukker ");
   }
          /**
     *  Scanner Pause
     */
   private void pause()
   {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
   }
             /**
     *  Write SearchMenu 
     */
   public int writeSearchMenu()
   {
       Scanner keyboard = new Scanner(System.in); 
       System.out.println("Tryk 1 for at søge med navn");
       System.out.println("Tryk 2 for at søge med ID");
       System.out.println("Tryk 3 for at gå tilbage");
        
       int choise = keyboard.nextInt();
       return choise;
   }
                /**
     *  Write UpdateMenu 
     */
   private int writeupdateMenu()
   {
       Scanner keyboard = new Scanner(System.in); 
       System.out.println("Tryk 1 for at updatere udfra navn");
       System.out.println("Tryk 2 for at updatere udfra ID");
       System.out.println("Tryk 3 for at gå tilbage");
        
       int choise = keyboard.nextInt();
       return choise;
   }

}
    